﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DatabaseHelper;

namespace UI.Online.SubForms
{
    public partial class frmNewProject : Form
    {
        public string ProjectLocation;
        public string ProjectName;

        public frmNewProject()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            // Show the FolderBrowserDialog.
            DialogResult result = fbdLocation.ShowDialog();
            if (result == DialogResult.OK)
            {
                ProjectLocation = fbdLocation.SelectedPath;
                txtProjectLocation.Text = ProjectLocation + @"\" + ProjectName; 
            }
        }

        private void txtProjectName_TextChanged(object sender, EventArgs e)
        {
            ProjectName = txtProjectName.Text;
            txtProjectLocation.Text = ProjectLocation + @"\" + ProjectName; 
        }

        private void AddNewProject(string ProjectName, string ProjectLocation)
        {
            if ((ProjectName != "") && (ProjectLocation != ""))
            {
                DBHelper dh = new DBHelper();
                PProject pp = new PProject();
                pp.ProjectName = ProjectName;
                pp.Location = ProjectLocation;

                dh.Query = "SELECT * FROM Projects WHERE ProjectName = '" + ProjectName + "'";
                DataTable dt = new DataTable();
                dt = dh.Fill();
                if (dt.Rows.Count == 0)
                {
                    dh.Add(pp);
                }
                else
                {
                    MessageBox.Show("The project '" + ProjectName + "' already exist.\n Please create new project with different name.",
                        "Duplucated projects",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation,
                        MessageBoxDefaultButton.Button1);
                }
            }
            else
            {
                MessageBox.Show("Project name and location must not be empty.");
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            AddNewProject(txtProjectName.Text, txtProjectLocation.Text);
            if(!System.IO.Directory.Exists(txtProjectLocation.Text))
            {
                try
                {
                    System.IO.Directory.CreateDirectory(txtProjectLocation.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            this.Close();
        }
    }
}
